import { message } from "ant-design-vue";
import axios, { AxiosRequestConfig, Method } from "axios";
import { throttle } from ".";
import router from "@/router";
import store from "@/store";
import { uniqueId } from "lodash-es";
import projectConfig from "@/config";
const request = axios.create({
    baseURL: process.env.VUE_APP_BASEURL,
    timeout: 1000 * 60 * 10,
    withCredentials: true,
});

interface RequestArrItem {
    url: string | undefined;
    method: Method | undefined;
    requesting: boolean;
    uid: string | number;
}
interface extendedConfig extends AxiosRequestConfig {
    uid: string | number;
}
const requestArr: RequestArrItem[] = [];

request.interceptors.request.use(
    config => {
        const uid = uniqueId(projectConfig.axiosUidPrefix);
        (config as extendedConfig).uid = uid;
        store.commit("changeRequesting", true);
        requestArr.push({ url: config.url, method: config.method, uid: uid, requesting: true });
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);
const dealRequestStatus = (url: RequestArrItem["url"], method: RequestArrItem["method"], uid: RequestArrItem["uid"]) => {
    if (requestArr.length) {
        const currentRequestItem = requestArr.find(item => item.url === url && item.method === method && item.uid === uid);
        if (currentRequestItem) {
            currentRequestItem.requesting = false;
        }
        store.commit(
            "changeRequesting",
            requestArr.some(item => item.requesting)
        );
    }
};
export const httpMessageThrottle = throttle((msg, type) => (type ? message[type](msg) : message.error(msg)), 500);
request.interceptors.response.use(
    response => {
        const {
            data: { code, message },
        } = response;
        const { url, method, uid } = response.config as extendedConfig;
        dealRequestStatus(url, method, uid);
        if (code === 400) {
            httpMessageThrottle(message || "参数错误");
        }
        if (code === 401) {
            httpMessageThrottle(message || "请先登录");
            console.log("未登录，重定向到登录");
            router.replace({ name: "login" });
        }
        if (code === 403) {
            httpMessageThrottle(message || "暂无权限");
        }
        if (code === 404) {
            httpMessageThrottle(message || "接口不存在");
        }
        if (code === 500) {
            httpMessageThrottle(message || "服务器错误");
        }
        return response.data as ResponseProps;
    },
    err => {
        const { url, method, uid } = err.config as extendedConfig;
        store.commit("changeError", err);
        httpMessageThrottle(err.message);
        dealRequestStatus(url, method, uid);
        return Promise.reject(err);
    }
);
export interface ResponseProps<T = any> {
    code: number;
    data?: T | any;
    message?: string;
}
const requestInstance = (config: AxiosRequestConfig<any>) => {
    return request(config);
};
requestInstance.get = request.get as <T = any, R = ResponseProps<T>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>;
requestInstance.post = request.post as <T = any, R = ResponseProps<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R>;
requestInstance.delete = request.delete as <T = any, R = ResponseProps<T>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>;
requestInstance.put = request.put as <T = any, R = ResponseProps<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R>;
requestInstance.patch = request.patch as <T = any, R = ResponseProps<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R>;
export default requestInstance;
